<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>一丁点冷饮</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">饮品名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入饮品名称"
                   value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">饮品单价</label>
        <div class="layui-input-block">
            <input type="text" name="price" lay-verify="required" lay-reqtext="单价不能为空" placeholder="请输入饮品单价"
                   value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">饮品分类</label>
        <div class="layui-input-block">
            <select name="categoryId" id="categorys" lay-verify="required">
                <option value="">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认添加</button>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery'], function(){
        let form = layui.form,
            $ = layui.jquery;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let params = JSON.stringify(data.field);

            // TODO: 调用创建饮品的API接口
            $.ajax({
                url: '/drink/',
                method: 'post',
                data: params,
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: (response) => {
                    // 关闭弹出窗口
                    let iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                }
            });

            return false;
        });

        // TODO: 调用获取所有分类的API接口
        //  获得分类数据后，渲染分类的Select列表
        $.ajax({
            url: '/category/',
            method: 'get',
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: (res) => {
                let list = res;
                let select = document.getElementById('categorys');
                if (list != null || list.size() > 0) {
                    list.forEach(c => {
                        let option = document.createElement("option");
                        option.setAttribute("value", c.id);
                        option.innerText = c.name;
                        select.appendChild(option);
                    });
                }
                form.render('select');
            }
        });

        form.render(); //更新全部
    });
</script>
</body>
</html>